<template>
  <!--审核中期检查-->
  <div class="mid-term">
    <InnerBreadcrumb v-if="!idTip" v-model="indexKey" :path="breadPath" :showBack="true" />

    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <!--查看学生信息弹框-->
      <StuInfoDialog :dialogVisible="dialogVisible" :studentForm="studentInfoForm" @stu-close="onClose"></StuInfoDialog>

      <condition-card @reset="reset" @search="searchList" label-width="140px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.grade" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.adminCollege')">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.studentColleges" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.major')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.reviewStatus')">
              <el-select v-model="queryForm.status" filterable class="search-item" clearable>
                <el-option v-for="item in source.statusOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectName')">
              <el-input v-model="queryForm.projectName" class="search-item" :placeholder="$t('graduationDesign.pleaseEnter')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.reviewMidTermCheck')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <el-input v-model="queryForm.studentId " :placeholder="$t('graduationDesign.pleaseEnterStuSearch')" style="width:300px" @keyup.enter.native="searchList" clearable>
              <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer" @click="searchList"></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
          </div>
        </div>

        <edu-table ref="reviewMidTermTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.college')" prop="collegeI18n" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.major')" prop="subjectIdI18n" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.direction')" prop="dirName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.student')" prop="studentName" min-width="110">
            <template slot-scope="scope">
              <el-button type="text" @click="onDetail(scope.row)">{{scope.row.studentName + '(' + scope.row.studentId + ')'}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.mentor')" prop="teacherName" min-width="110"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.reviewStatus')" min-width="100">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 'WaitCommit'" type="info">{{$t('graduationDesign.draft')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Process'" type="info">{{$t('graduationDesign.pendingReview')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Reject'" type="danger">{{$t('graduationDesign.rejected')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'End'" type="success">{{$t('graduationDesign.passed')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onReview(scope.row)" v-show="scope.row.status === 'Process'">{{$t('graduationDesign.review')}}</el-button>
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>

    <div v-if="indexKey === 'VIEW' || indexKey === 'REVIEW'">
      <!--审核/查看-->
      <MidTermCheckInfo :actionType="actionType" :progressData="progressData" :midTermForm="midTermForm" :studentForm="studentForm" @mid-term-pass="onPass" @mid-term-reject="onReject" @mid-term-cancel="onCancel"></MidTermCheckInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/midTermCheck"
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'reviewMidTermCheck',
  props: {
    dataInfo: {
      type: Object,
      default: {}
    },
    idTip: {
      type: String,
    },
  },
  data() {
    return {
      api: API,
      getList: '/api/designservice/docMidCheckAudit/queryByPage',
      queryForm: {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectName: '',
        studentId: '',
        status: ''
      },
      loading: false,
      indexKey:'MAIN',
      source: {
        studentColleges: [], // 行政学院
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        // 审核状态
        statusOptions: [{
          label: "待审核",
          value: "Process"
        }, {
          label: "已驳回",
          value: "Reject"
        }, {
          label: "已通过",
          value: "End"
        }]
      },
      breadPath: {
        label: this.$t('graduationDesign.reviewMidTermCheck'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.viewMidTermCheck')
          },
          {
            key: "REVIEW",
            label: this.$t('graduationDesign.reviewMidTermCheck')
          }
        ]
      },
      selectedData: [], // 勾选的列表数据
      actionType: '',
      midTermForm: {},
      studentForm: {},
      progressData: [], //审核进度列表
      dialogVisible: false,
      studentInfoForm: {}
    };
  },
  mounted() {
    this.queryForm.grade = new Date().getFullYear() + '';
    this.getCollege();
    this.searchList();
    tools.initAllCollege(this.source.studentColleges);
    if(this.dataInfo&&this.dataInfo.no){
      this.indexKey=this.$route.query.indexKey;
      this.actionType=this.$route.query.actionType;
      this.midTermForm=this.dataInfo.content;
      this.studentForm=this.dataInfo.content;
      if(this.dataInfo.content.subjectIdI18n){
        this.studentForm.subjectName =this.dataInfo.content.subjectIdI18n;
      }
      this.midTermForm.flowNo=this.dataInfo.no;
      this.progressData=this.dataInfo.listApproveHistory;
    }
  },
  methods: {
    searchList() {
      this.$refs.reviewMidTermTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectName: '',
        studentId: '',
        status: ''
      };
      this.queryForm.grade = new Date().getFullYear() + '';
      this.source.subjectOptions = [];
      this.searchList();
    },
    handleSelectChange(val) {
      this.selectedData = val;
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    // 选择年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.grade) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查学生信息
    getStudentInfo(params) {
      return this.api.getStudentInfo(params).then(res => {
        return res.data;
      }).catch(() => { });
    },
    // 查学生课题信息
    getSubjectDetail(params = {}) {
      return this.api.getSubjectDetail(params).then(({ data }) => {
        return data[0];
      });
    },
    // 查审核进度
    getReviewProgress(applyNo) {
      this.api.getProgress(applyNo).then(res => {
        this.progressData = res.data.listApproveHistory;
      }).catch();
    },
    // 审核
    async onReview(row) {
      this.actionType = "REVIEW";
      const params = {
        studentId: row.studentId
      };
      this.studentForm = await this.getStudentInfo(params);
      const subjectInfo = await this.getSubjectDetail({ ...row });
      this.indexKey = 'REVIEW';
      this.midTermForm = Object.assign({}, subjectInfo, { ...row });
      this.progressData = [];
      if (row.flowNo) {
        this.getReviewProgress(row.flowNo);
      }
    },
    // 查看
    async onView(row) {
      this.actionType = "REVIEW_VIEW";
      const params = {
        studentId: row.studentId
      };
      this.studentForm = await this.getStudentInfo(params);
      const subjectInfo = await this.getSubjectDetail({ ...row });
      this.indexKey = 'VIEW';
      this.midTermForm = Object.assign({}, subjectInfo, { ...row });
      this.progressData = [];
      if (row.flowNo) {
        this.getReviewProgress(row.flowNo);
      }
    },
    // 查看学生信息
    async onDetail(row) {
      const params = {
        studentId: row.studentId
      };
      this.studentInfoForm = await this.getStudentInfo(params);
      this.dialogVisible = true;
    },
    onClose() {
      this.dialogVisible = false;
    },
    // 通过/驳回提交
    handleSubmit(data, type) {
      this.api.approval(data).then(res => {
        this.indexKey = "MAIN";
        this.$message.success((type === "End") ? this.$t('graduationDesign.passed') : this.$t('graduationDesign.rejected'));
        this.searchList();
      }).catch(() => { });
    },
    // 通过开题报告
    onPass(data) {
      this.handleSubmit(data, "End");
    },
    // 驳回开题报告
    onReject(data) {
      this.handleSubmit(data, "Reject");
    },
    onCancel() {
      this.indexKey = "MAIN";
      this.searchList();
    },
    // 导出
    onExport() {
      const params = Object.assign(this.queryForm, { queryType: 2, list: this.selectedData });
      this.api.exportPdf(params).then(res => {
        // this.delayRequest(res.key);
        tools.downloadFile(res.key, "审核中期检查.zip", this.getMidExportStatus, this)
      });
    },
    getMidExportStatus(key) {
      return this.api.getExportStatus(key).then(res => {
        return res;
      }).catch(err => {
        this.loading = false;
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      const resData = await this.getMidExportStatus(key);
      if (resData.code === 200 && resData.data.status) {
        this.loading = false;
        window.open("/api/designservice/resultPublish/downLoadMore?path=" + resData.data.path + '&fileName=docMidCheck.zip');
        //
      } else {
        this.delayRequest(key);
      }
    }
  },
  components: {
    MidTermCheckInfo: () => import('../components/midTermCheckInfo'),
    StuInfoDialog: () => import('../components/stuInfoDialog')
  }
}
</script>
<style lang="scss" scoped>
.mid-term {
    .search-item {
        width: 100%;
    }
}
</style>
